/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import Controllers.*;
import Controllers.exceptions.IllegalOrphanException;
import Controllers.exceptions.NonexistentEntityException;
import javax.swing.*;
import javax.persistence.*;
import Entities.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.table.AbstractTableModel;
import javax.swing.ListModel;
import javax.swing.table.TableModel;

/**
 *
 * @author Heinz Hernandez
 */
public class JFrmMain extends javax.swing.JFrame {

    private JPnlReusable pnlVet;
    private JPnlReusable pnlOwner;
    private JPnlPets pnlPets;
    private List<Person> listVets;
    private List<Person> listOwners;
    private List<Pet> listPets;
    private List<Pet> petList;
    private List<Pet> petList2;
    private BufferedImage img;
    PersonTableModel model;
    OwnerTableModel ownModel;
    PetTableModel petModel;
    DefaultListModel vacListModel;
    DefaultListModel AppListModel;
    DefaultComboBoxModel comboPetsModel;
    DefaultComboBoxModel comboPetsModel2;
    EntityManagerFactory emFactory = Persistence.createEntityManagerFactory("HPVAS_HPU");
    EntityManager em = emFactory.createEntityManager();
    PersonJpaController perCtrl = new PersonJpaController(emFactory);
    EntityManagerFactory emFactory2 = Persistence.createEntityManagerFactory("HPVAS_HPU");
    EntityManager em2 = emFactory.createEntityManager();
    PersonJpaController perCtrl2 = new PersonJpaController(emFactory2);
    PetJpaController petCtrl = new PetJpaController(emFactory);
    PetJpaController petCtrl2 = new PetJpaController(emFactory);
    LoadData dataloader;

    /**
     * Creates new form JFrmMain
     */
    public JFrmMain() {
        initComponents();


        dataloader = new LoadData();
        dataloader.loadVetsInDB();
        dataloader.loadOwnersInDB();
        dataloader.loadPetsInDB();
        dataloader.loadRecordsInDB();
        dataloader.loadAppointmentsInDB();
        dataloader.loadVaccinesInDB();
        pnlVet = new JPnlReusable();
        pnlOwner = new JPnlReusable();
        pnlPets = new JPnlPets();
        this.jTabbedPane1.add("VETERINARIES", pnlVet);
        this.jTabbedPane1.add("OWNERS", pnlOwner);
        this.jTabbedPane1.add("PETS", pnlPets);

        pnlVet.jComboBox1.removeAllItems();
        pnlOwner.jComboBox1.removeAllItems();
        clearPetFields();
        clearPetFields2();
        pnlVet.jLabelPetImg.setText("");
        pnlOwner.jLabelPetImg.setText("");
        pnlVet.jLabel5.setVisible(false);
        pnlVet.textPetTotal.setVisible(false);

        pnlVet.jButton1.setEnabled(false);
        pnlVet.jButton2.setEnabled(false);
        pnlVet.jButton3.setEnabled(false);
        pnlOwner.jButton1.setEnabled(false);
        pnlOwner.jButton2.setEnabled(false);
        pnlOwner.jButton3.setEnabled(false);
        pnlPets.jButton1.setEnabled(false);
        pnlPets.jButton2.setEnabled(false);
        pnlPets.jButton3.setEnabled(false);

        loadVetTable();
        loadOwnerTable();
        loadPetsTable();


        pnlVet.jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonPnlVet1ActionPerformed(evt);
            }
        });

        pnlVet.jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonPnlVet2ActionPerformed(evt);
            }
        });

        pnlVet.jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTable1VetMouseClicked(evt);
            }
        });

        pnlVet.jComboBox1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                jComboPetsSelectionChangeCommited(evt);
            }
        });


        pnlVet.jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                try {
                    jButtonPnlVet3ActionPerformed(evt);
                } catch (NonexistentEntityException ex) {
                    Logger.getLogger(JFrmMain.class.getName()).log(Level.SEVERE, null, ex);
                } catch (IllegalOrphanException ex) {
                    Logger.getLogger(JFrmMain.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
        
        //<editor-fold defaultstate="collapsed" desc=" JEISSON CARDENAS ">
        // LISTENERS PARA OBJETOS DE LA PESTAÑA OWNER

        pnlOwner.jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonPnlOwner1ActionPerformed(evt);
            }
        });

        pnlOwner.jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonPnlOwner2ActionPerformed(evt);
            }
        });

        pnlOwner.jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTable1OwnerMouseClicked(evt);
            }
        });

        pnlOwner.jComboBox1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                jComboOwnerPetsSelectionChangeCommited(evt);
            }
        });


        pnlOwner.jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                try {
                    jButtonPnlOwner3ActionPerformed(evt);
                } catch (NonexistentEntityException ex) {
                    Logger.getLogger(JFrmMain.class.getName()).log(Level.SEVERE, null, ex);
                } catch (IllegalOrphanException ex) {
                    Logger.getLogger(JFrmMain.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
        //</editor-fold>
        
        //<editor-fold defaultstate="collapsed" desc=" GERMAN FLOREZ ">
            // LISTENERS PARA EL PANEL PETS

        pnlPets.jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTable1PnlPetsMouseClicked(evt);
            }
        });

        pnlPets.jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                try {
                    jButtonPnlPets3ActionPerformed(evt);
                } catch (NonexistentEntityException ex) {
                    Logger.getLogger(JFrmMain.class.getName()).log(Level.SEVERE, null, ex);
                } catch (IllegalOrphanException ex) {
                    Logger.getLogger(JFrmMain.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });

        //</editor-fold>

        
    }// end costructor

    private void jTable1VetMouseClicked(java.awt.event.MouseEvent evt) {
        Person tmpPer = new Person();
        Long id = (Long) model.getValueAt(pnlVet.jTable1.getSelectedRow(), 0);
        tmpPer = perCtrl.findPerson(id);
        petList = em.createNamedQuery("Pet.findByPerId")
                .setParameter("perId", tmpPer)
                .getResultList();

        pnlVet.jComboBox1.removeAllItems();

        comboPetsModel = new DefaultComboBoxModel();
        for (Pet pet : petList) {
            comboPetsModel.addElement(pet);
        }
        pnlVet.jComboBox1.setModel(comboPetsModel);
        loadPetFields();

    }

    private void jButtonPnlVet1ActionPerformed(java.awt.event.ActionEvent evt) {
        JFrmRegisterVet createVet = new JFrmRegisterVet(this);
        createVet.setVisible(true);

    }

    private void jButtonPnlVet2ActionPerformed(java.awt.event.ActionEvent evt) {

        if (pnlVet.jTable1.getSelectedRow() != -1) {
            Person tmpPer = new Person();
            Long id = (Long) model.getValueAt(pnlVet.jTable1.getSelectedRow(), 0);
            tmpPer = perCtrl.findPerson(id);
            JFrmRegisterVet modifyVet = new JFrmRegisterVet(tmpPer, this);
            modifyVet.setVisible(true);

        }

    }

    private void jButtonPnlVet3ActionPerformed(java.awt.event.ActionEvent evt) throws NonexistentEntityException, IllegalOrphanException {
        //Person tmpPer = new Person();
        Long id = (Long) model.getValueAt(pnlVet.jTable1.getSelectedRow(), 0);
//        tmpPer = perCtrl.findPerson(id);
//        JFrmRegisterVet modifyVet= new JFrmRegisterVet(tmpPer);
//        modifyVet.setVisible(true);
        perCtrl.destroy(id);
        JOptionPane.showMessageDialog(null, "The selected register was deleted from the Database");
        loadVetTable();

    }
    
    //<editor-fold defaultstate="collapsed" desc=" JEISON CARDENAS ">

    private void jTable1OwnerMouseClicked(java.awt.event.MouseEvent evt) {
        Person tmpPer = new Person();
        Long id = (Long) ownModel.getValueAt(pnlOwner.jTable1.getSelectedRow(), 0);
        tmpPer = perCtrl2.findPerson(id);
        petList2 = em.createNamedQuery("Pet.findByPerIdOwn")
                .setParameter("perIdown", tmpPer)
                .getResultList();

        pnlOwner.jComboBox1.removeAllItems();

        comboPetsModel2 = new DefaultComboBoxModel();
        for (Pet pet : petList2) {
            comboPetsModel2.addElement(pet);
        }
        pnlOwner.jComboBox1.setModel(comboPetsModel2);
        loadPetFields2();

    }

    private void jComboOwnerPetsSelectionChangeCommited(java.awt.event.ActionEvent evt) {
        loadPetFields2();


    }

    private void jButtonPnlOwner1ActionPerformed(java.awt.event.ActionEvent evt) {
        JFrmRegisterOwner createOwner = new JFrmRegisterOwner(this);
        createOwner.setVisible(true);

    }

    private void jButtonPnlOwner2ActionPerformed(java.awt.event.ActionEvent evt) {

        if (pnlOwner.jTable1.getSelectedRow() != -1) {
            Person tmpPer = new Person();
            Long id = (Long) ownModel.getValueAt(pnlOwner.jTable1.getSelectedRow(), 0);
            tmpPer = perCtrl2.findPerson(id);
            JFrmRegisterOwner modifyOwner = new JFrmRegisterOwner(tmpPer, this);
            modifyOwner.setVisible(true);

        }

    }

    private void jButtonPnlOwner3ActionPerformed(java.awt.event.ActionEvent evt) throws NonexistentEntityException, IllegalOrphanException {
        //Person tmpPer = new Person();
        Long id = (Long) ownModel.getValueAt(pnlOwner.jTable1.getSelectedRow(), 0);
//        tmpPer = perCtrl.findPerson(id);
//        JFrmRegisterVet modifyVet= new JFrmRegisterVet(tmpPer);
//        modifyVet.setVisible(true);
        perCtrl2.destroy(id);
        JOptionPane.showMessageDialog(null, "The selected register was deleted from the Database");
        loadOwnerTable();

    }
    //</editor-fold>
    
    //<editor-fold defaultstate="collapsed" desc=" GERMAN FLOREZ ">
    
    private void jTable1PnlPetsMouseClicked(java.awt.event.MouseEvent evt) {

        vacListModel = new DefaultListModel();
        AppListModel = new DefaultListModel();





        if (pnlPets.jTable1.getSelectedRow() != -1) {

            Pet tmpPet = new Pet();
            Long id = (Long) petModel.getValueAt(pnlPets.jTable1.getSelectedRow(), 0);

            tmpPet = petCtrl.findPet(id);

            try {
                img = ImageIO.read(new ByteArrayInputStream(tmpPet.getPetImg()));
            } catch (IOException ex) {
                Logger.getLogger(JFrmMain.class.getName()).log(Level.SEVERE, null, ex);
            }
            ImageIcon image = new ImageIcon(img);
            pnlPets.jLabelPetImg.setIcon(image);


            List<Record> tmpList;

            //Person tmpPer = new Person();
            //Long id = (Long) ownModel.getValueAt(pnlOwner.jTable1.getSelectedRow(), 0);
            //tmpPer = perCtrl2.findPerson(id);
            tmpList = em2.createNamedQuery("Record.findByPetId")
                    .setParameter("petId", tmpPet)
                    .getResultList();

            Record obtainedRecord = new Record();

            for (Record rec : tmpList) {
                if (rec.getPetId().getPetId() == tmpPet.getPetId()) {
                    obtainedRecord = rec;
                }
            }

            List<Appointment> aptList = obtainedRecord.getAppointmentList();
            List<Vacine> vacList = obtainedRecord.getVacineList();

            for (Appointment app : aptList) {
                AppListModel.addElement(app.toString());
            }

            for (Vacine vac : vacList) {
                vacListModel.addElement(vac.toString());
            }

            pnlPets.JlistApp.setModel(AppListModel);
            pnlPets.JlistVac.setModel(vacListModel);


        }

    }

    //ESTE METODO SE EJECUTA AL HACER CLICK EN EL BOTON CREAR DEL PANEL VETERIANRIOS
    private void jButtonPnlPets3ActionPerformed(java.awt.event.ActionEvent evt) throws NonexistentEntityException, IllegalOrphanException {
        //Person tmpPer = new Person();
        Long id = (Long) petModel.getValueAt(pnlPets.jTable1.getSelectedRow(), 0);
//        tmpPer = perCtrl.findPerson(id);
//        JFrmRegisterVet modifyVet= new JFrmRegisterVet(tmpPer);
//        modifyVet.setVisible(true);
        petCtrl.destroy(id);
        JOptionPane.showMessageDialog(null, "The selected register was deleted from the Database");
        loadPetsTable();

    }

    private void jComboPetsSelectionChangeCommited(java.awt.event.ActionEvent evt) {
        loadPetFields();


    }
    
    //</editor-fold>

    

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jButton1 = new javax.swing.JButton();
        textSearch = new javax.swing.JTextField();
        JbtnSearch = new javax.swing.JButton();
        label11 = new java.awt.Label();
        JbtnLogin = new javax.swing.JButton();
        txtName = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        txtLastName = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        txtPass = new javax.swing.JPasswordField();
        jLabel5 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setLocationByPlatform(true);
        setMaximumSize(new java.awt.Dimension(800, 600));
        setMinimumSize(new java.awt.Dimension(800, 600));
        setPreferredSize(new java.awt.Dimension(800, 600));
        setResizable(false);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel1.setText("WELCOME GUEST");

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 20)); // NOI18N
        jLabel2.setText("HPVAS 2013");

        jButton1.setText("EXIT");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        JbtnSearch.setText("SEARCH");
        JbtnSearch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                JbtnSearchActionPerformed(evt);
            }
        });

        label11.setText(":");

        JbtnLogin.setText("LOGIN");
        JbtnLogin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                JbtnLoginActionPerformed(evt);
            }
        });

        txtName.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtNameActionPerformed(evt);
            }
        });

        jLabel3.setText("NAME");

        jLabel4.setText("LAST NAME");

        jLabel5.setText("PASSWORD");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jTabbedPane1)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jLabel2))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(textSearch, javax.swing.GroupLayout.PREFERRED_SIZE, 329, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(JbtnSearch)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jButton1))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtName, javax.swing.GroupLayout.PREFERRED_SIZE, 112, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtLastName, javax.swing.GroupLayout.PREFERRED_SIZE, 112, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtPass, javax.swing.GroupLayout.DEFAULT_SIZE, 109, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(JbtnLogin)
                        .addGap(12, 12, 12)))
                .addContainerGap())
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(297, 297, 297)
                    .addComponent(label11, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(298, Short.MAX_VALUE)))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3)
                    .addComponent(txtLastName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4)
                    .addComponent(jLabel5)
                    .addComponent(txtPass, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(JbtnLogin))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 387, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(textSearch, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(JbtnSearch))
                .addContainerGap())
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(194, 194, 194)
                    .addComponent(label11, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(301, Short.MAX_VALUE)))
        );

        jLabel1.getAccessibleContext().setAccessibleName("JlWelcome");
        jButton1.getAccessibleContext().setAccessibleName("JbtnExit");
        textSearch.getAccessibleContext().setAccessibleName("JtxtSearch");
        JbtnSearch.getAccessibleContext().setAccessibleName("JbtnSearch");

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void JbtnSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_JbtnSearchActionPerformed

        if (this.jTabbedPane1.getSelectedIndex() == 0) {
            loadVetsByNameLike();
        }

        if (this.jTabbedPane1.getSelectedIndex() == 1) {
            loadOwnersByNameLike();
        }
        if (this.jTabbedPane1.getSelectedIndex() == 2) {
            loadPetsByNameLike();
        }




    }//GEN-LAST:event_JbtnSearchActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        System.exit(0);
    }//GEN-LAST:event_jButton1ActionPerformed

    private void txtNameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtNameActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtNameActionPerformed

    private void JbtnLoginActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_JbtnLoginActionPerformed
       
        //<editor-fold defaultstate="collapsed" desc=" IVAN DUQUE ">
        if (!"".equals(txtName.getText()) && !"".equals(txtLastName.getText()) && !"".equals(txtPass.getText())) {
            Login login = new Login(txtName.getText(), txtLastName.getText(), txtPass.getText());

            if (login.verifyLoginData()) {
                JOptionPane.showMessageDialog(null, "Now you're logged as: " + txtName.getText() + " " + txtLastName.getText(), "LOGIN SUCCESFULL", JOptionPane.INFORMATION_MESSAGE);
                enableButtons();
                jLabel1.setText("Welcome Employee: " + txtName.getText() + " " + txtLastName.getText());

            } else {

                JOptionPane.showMessageDialog(null, "Invalid Login information", "LOGIN ERROR", JOptionPane.ERROR_MESSAGE);

            }


        } else {

            JOptionPane.showMessageDialog(null, "Please fill the empty fields", "LOGIN ERROR", JOptionPane.ERROR_MESSAGE);

        }
        //</editor-fold>

    }//GEN-LAST:event_JbtnLoginActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(JFrmMain.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(JFrmMain.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(JFrmMain.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(JFrmMain.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new JFrmMain().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton JbtnLogin;
    private javax.swing.JButton JbtnSearch;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JTabbedPane jTabbedPane1;
    private java.awt.Label label11;
    private javax.swing.JTextField textSearch;
    private javax.swing.JTextField txtLastName;
    private javax.swing.JTextField txtName;
    private javax.swing.JPasswordField txtPass;
    // End of variables declaration//GEN-END:variables

    public void loadVetTable() {
        EntityManagerFactory emFactory = Persistence.createEntityManagerFactory("HPVAS_HPU");
        EntityManager em = emFactory.createEntityManager();
        PersonJpaController perCtrl = new PersonJpaController(emFactory);
        listVets = em.createNamedQuery("Person.findByPerType")
                .setParameter("perType", "veterinary")
                .getResultList();

        //listVets = perCtrl.findPersonEntities();
        model = new PersonTableModel(listVets);
        pnlVet.jTable1.setModel(model);
    }

    private void loadVetsByNameLike() {
        EntityManagerFactory emFactory = Persistence.createEntityManagerFactory("HPVAS_HPU");
        EntityManager em = emFactory.createEntityManager();
        PersonJpaController perCtrl = new PersonJpaController(emFactory);
        listVets = em.createNamedQuery("Person.findPerByTypeAndNameLike")
                .setParameter("perType", "veterinary")
                .setParameter("perName", "%" + textSearch.getText().trim() + "%")
                .getResultList();

        //listVets = perCtrl.findPersonEntities();
        model = new PersonTableModel(listVets);
        pnlVet.jTable1.setModel(model);

    }

    //<editor-fold defaultstate="collapsed" desc=" JEISON CARDENAS ">
    public void loadOwnerTable() {
        EntityManagerFactory emFactory = Persistence.createEntityManagerFactory("HPVAS_HPU");
        EntityManager em = emFactory.createEntityManager();
        PersonJpaController perCtrl = new PersonJpaController(emFactory);
        listOwners = em.createNamedQuery("Person.findByPerType")
                .setParameter("perType", "owner")
                .getResultList();

        //listVets = perCtrl.findPersonEntities();
        ownModel = new OwnerTableModel(listOwners);
        pnlOwner.jTable1.setModel(ownModel);
    }

    private void loadOwnersByNameLike() {
        EntityManagerFactory emFactory = Persistence.createEntityManagerFactory("HPVAS_HPU");
        EntityManager em = emFactory.createEntityManager();
        PersonJpaController perCtrl = new PersonJpaController(emFactory);
        listOwners = em.createNamedQuery("Person.findPerByTypeAndNameLike")
                .setParameter("perType", "owner")
                .setParameter("perName", "%" + textSearch.getText().trim() + "%")
                .getResultList();

        //listVets = perCtrl.findPersonEntities();
        ownModel = new OwnerTableModel(listOwners);
        pnlOwner.jTable1.setModel(ownModel);

    }

    private void loadPetFields2() {
        if (pnlOwner.jComboBox1.getSelectedItem() != null) {
            Pet selectedPet = new Pet();
            selectedPet = (Pet) pnlOwner.jComboBox1.getSelectedItem();
            pnlOwner.textPetName.setText(selectedPet.getPetName());
            pnlOwner.textPetSpecs.setText(selectedPet.getPetSpecies());
            pnlOwner.textPetAge.setText(Integer.toString(selectedPet.getPetAge()));
            pnlOwner.textPetWeight.setText(Double.toString(selectedPet.getPetWeight()));
            try {
                img = ImageIO.read(new ByteArrayInputStream(selectedPet.getPetImg()));
            } catch (IOException ex) {
                Logger.getLogger(JFrmMain.class.getName()).log(Level.SEVERE, null, ex);
            }
            ImageIcon image = new ImageIcon(img);
            pnlOwner.jLabelPetImg.setIcon(image);

            List<Record> tmpList;

            //Person tmpPer = new Person();
            //Long id = (Long) ownModel.getValueAt(pnlOwner.jTable1.getSelectedRow(), 0);
            //tmpPer = perCtrl2.findPerson(id);
            tmpList = em2.createNamedQuery("Record.findByPetId")
                    .setParameter("petId", selectedPet)
                    .getResultList();

            Record obtainedRecord = new Record();

            for (Record rec : tmpList) {
                if (rec.getPetId().getPetId() == selectedPet.getPetId()) {
                    obtainedRecord = rec;
                }
            }

            List<Appointment> aptList = obtainedRecord.getAppointmentList();

            int total = 0;

            if (aptList != null) {
                for (Appointment ap : aptList) {
                    total += ap.getAppoCharge();
                }
            }

            pnlOwner.textPetTotal.setText(String.valueOf(total));

        } else {
            clearPetFields2();

        }
    }

    private void clearPetFields2() {
        pnlOwner.textPetName.setText("");
        pnlOwner.textPetSpecs.setText("");
        pnlOwner.textPetAge.setText("");
        pnlOwner.textPetWeight.setText("");
        pnlOwner.jLabelPetImg.setIcon(null);
        pnlOwner.textPetTotal.setText("");
    }

    //</editor-fold>
    
    //<editor-fold defaultstate="collapsed" desc=" GERMAN FLOREZ ">
    public void loadPetsTable() {
        EntityManagerFactory emFactory = Persistence.createEntityManagerFactory("HPVAS_HPU");
        EntityManager em = emFactory.createEntityManager();
        PetJpaController petCtrl = new PetJpaController(emFactory);
        listPets = em.createNamedQuery("Pet.findAll")
                .getResultList();

        //listVets = perCtrl.findPersonEntities();
        petModel = new PetTableModel(listPets);
        pnlPets.jTable1.setModel(petModel);
    }

    private void loadPetsByNameLike() {
        EntityManagerFactory emFactory = Persistence.createEntityManagerFactory("HPVAS_HPU");
        EntityManager em = emFactory.createEntityManager();
        PetJpaController perCtrl = new PetJpaController(emFactory);
        listPets = em.createNamedQuery("Pet.findByPetNameLike")
                .setParameter("petName", "%" + textSearch.getText().trim() + "%")
                .getResultList();

        //listVets = perCtrl.findPersonEntities();
        petModel = new PetTableModel(listPets);
        pnlPets.jTable1.setModel(petModel);

    }

    private void loadPetFields() {
        if (pnlVet.jComboBox1.getSelectedItem() != null) {
            Pet selectedPet = new Pet();
            selectedPet = (Pet) pnlVet.jComboBox1.getSelectedItem();
            pnlVet.textPetName.setText(selectedPet.getPetName());
            pnlVet.textPetSpecs.setText(selectedPet.getPetSpecies());
            pnlVet.textPetAge.setText(Integer.toString(selectedPet.getPetAge()));
            pnlVet.textPetWeight.setText(Double.toString(selectedPet.getPetWeight()));
            try {
                img = ImageIO.read(new ByteArrayInputStream(selectedPet.getPetImg()));
            } catch (IOException ex) {
                Logger.getLogger(JFrmMain.class.getName()).log(Level.SEVERE, null, ex);
            }
            ImageIcon image = new ImageIcon(img);
            pnlVet.jLabelPetImg.setIcon(image);

        } else {
            clearPetFields();

        }
    }

    private void clearPetFields() {
        pnlVet.textPetName.setText("");
        pnlVet.textPetSpecs.setText("");
        pnlVet.textPetAge.setText("");
        pnlVet.textPetWeight.setText("");
        pnlVet.jLabelPetImg.setIcon(null);
    }

    //</editor-fold>
    
    //<editor-fold defaultstate="collapsed" desc=" IVAN DUQUE ">
    private void enableButtons() {
        pnlVet.jButton1.setEnabled(true);
        pnlVet.jButton2.setEnabled(true);
        pnlVet.jButton3.setEnabled(true);
        pnlOwner.jButton1.setEnabled(true);
        pnlOwner.jButton2.setEnabled(true);
        pnlOwner.jButton3.setEnabled(true);
        pnlPets.jButton1.setEnabled(true);
        pnlPets.jButton2.setEnabled(true);
        pnlPets.jButton3.setEnabled(true);

    }
    //</editor-fold>

    
}
